﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Silverlight.Weblog.Client.CoreBL.Widgets;
using Silverlight.Weblog.Client.DAL.Theme;
using Silverlight.Weblog.Client.Default.Widgets.DefaultTheme;
using Silverlight.Weblog.Common.IoC;
using Silverlight.Weblog.Shared.Common.Exception;

namespace Silverlight.Weblog.Client.Default.Widgets
{
    [Export(typeof(ILayoutManager))]
    public partial class DefaultLayoutManagerControl : UserControl, ILayoutManager, IScrollToTop
    {
        public DefaultLayoutManagerControl()
        {
            IoC.Register<IScrollToTop>(this);

            // Required to initialize variables
            InitializeComponent();
        }




        #region Implementation of ITheme

        public void AddChild(IBlogWidget widget)
        {
            IDefaultLayoutWidget layoutElementToAddToGrid = widget as IDefaultLayoutWidget;
            ILayoutRegion possibleRegionForWidget =
                LayoutRegions.FirstOrDefault(region => region.IsWidgetForRegion(widget));

            if (possibleRegionForWidget != null)
            {
                possibleRegionForWidget.AddChild(widget);
            }
            else if (layoutElementToAddToGrid != null)
            {
                if (layoutElementToAddToGrid.IsActive)
                    AddWidgetControlToGrid(layoutElementToAddToGrid);

                layoutElementToAddToGrid.IsActiveChanged += new EventHandler(themeElementToAddToGrid_IsActiveChanged);
            }
            else
            {
                throw new SilverlightBlogException(widget.GetType().Name + " does not implement IDefaultLayoutWidget or IDefaultSidebarWidget and cannot be nested in the default SilverlightBlog Theme. So, implement IDefaultLayoutWidget.");
            }
        }

        public void DoneAddingChildren()
        {
            foreach (ILayoutRegion layoutRegion in LayoutRegions)
            {
                if (layoutRegion is IDefaultLayoutWidget)
                    AddWidgetControlToGrid((IDefaultLayoutWidget)layoutRegion);
                layoutRegion.DoneAddingChildren();
            }
        }

        public ILayoutRegion[] LayoutRegions { get; set; }

        void themeElementToAddToGrid_IsActiveChanged(object sender, EventArgs e)
        {
            IDefaultLayoutWidget layoutWidgetToEitherRemoveOrAdd = (IDefaultLayoutWidget)sender;

            if (layoutWidgetToEitherRemoveOrAdd.IsActive)
                AddWidgetControlToGrid(layoutWidgetToEitherRemoveOrAdd);
            else
                RemoveWidgetControlFromGrid(layoutWidgetToEitherRemoveOrAdd);
        }

        private void RemoveWidgetControlFromGrid(IDefaultLayoutWidget widget)
        {
            LayoutRoot.Children.Remove(widget.Control);
        }

        private void AddWidgetControlToGrid(IDefaultLayoutWidget layoutElementToAddToGrid)
        {
            Grid.SetRow(layoutElementToAddToGrid.Control, layoutElementToAddToGrid.Row);
            Grid.SetColumn(layoutElementToAddToGrid.Control, layoutElementToAddToGrid.Column);
            Grid.SetColumnSpan(layoutElementToAddToGrid.Control, layoutElementToAddToGrid.ColumnSpan);
            Grid.SetRowSpan(layoutElementToAddToGrid.Control, layoutElementToAddToGrid.RowSpan);
            layoutElementToAddToGrid.Control.VerticalAlignment = System.Windows.VerticalAlignment.Top;
            LayoutRoot.Children.Add(layoutElementToAddToGrid.Control);
        }

        #endregion

        #region Implementation of IScrollToTop

        public void ScrollToTop()
        {
            scrollViewer.ScrollToVerticalOffset(1);
        }

        #endregion
    }
}